#include "app.h"
#include "bsp_led188.h"
#include "stdio.h"
#include "OLED.h"
//创建温度变量，默认为100
uint8_t g_run_mode = 0; // 0温度，1时间,2自定义
int16_t g_LED188_temperature = 100; // [80,199]
int16_t g_LED188_time = 45;	// [1,60]
int16_t g_LED188_custom = 60; //
void App_display_init(){
	#if BUSINESS == 1
		LED188_init();
		//设置显示数字温度
		LED188_set_num(g_LED188_temperature);
		LED188_set_percent(0);
		LED188_set_charge(1);
	#elif BUSINESS == 2 || BUSINESS == 3
		OLED_Init();
	#endif
}
void App_display_task(){
	//INTERVAL_CHECK(2); //任务管理中心已经设置间隔
	LED188_display();
}

// oled需要刷新标记
uint8_t g_oled_refresh_flag = 1;
//数组的索引，由按键来修改
uint8_t g_motor_mode = 0; // Mode 1, 2, 3 范围：
uint8_t g_motor_force = 0; // 0: Low, 1: Medium, 2: High
uint8_t g_motor_status = 0; // 0: Stop, 1: Run
// 声明字符串数组
char* motor_mode[3] = {"1", "2", "3"};
char* motor_force[3] = {"Low", "Medium", "High"};
char* motor_status[2] = {"Stop", "Run"};


void App_display_OLED_task(){
	//printf("I2C OLED\r\n");
	if(g_oled_refresh_flag == 0)return;
	//OLED_Clear();
	OLED_ShowString(1,1,"Mode:");//静态
	OLED_ShowString(1,9,motor_mode[g_motor_mode]);//动态
	OLED_ShowString(2,1,"Force");//静态
	OLED_ShowString(2,12,"   ");//用于区域清除
	OLED_ShowString(2,9,motor_force[g_motor_force]);//动态
	OLED_ShowString(3,1,"Status");//静态
	OLED_ShowString(3,12," ");//用于区域清除
	OLED_ShowString(3,9,motor_status[g_motor_status]);//动态
	g_oled_refresh_flag = 0;
}

//创建密码锁OLED显示任务
//不断根据密码锁的状态来显示

/**************************************
1. 默认状态
Locked
2. 触摸输密码：按下'*'回退
用户触摸数字，输入密码，每触摸一次，屏幕上多一个*
Locked
Enter PIN: ******
3. 密码确认：按下'#'键
根据预设的密码进行验证
- 验证成功：屏幕显示 Unlocked!
 保持一段时间，回到待机状态
- 密码错误：屏幕显示 Try Again
- 连续3次错误：屏幕锁定 Locked in 10 sec
- 10秒倒计数后解除锁定（此时间内按键无反应）
***************************************/
char strbuf[32];

static uint8_t sec_display = 10;

#define Delay_check(interval)  \
static volatile uint64_t display_tick = 0;               \
if(display_tick == 0){                                   \
				display_tick = tasks_tick_get();         \
				return;                                  \
			}                                            \
			if(tasks_tick_get()-display_tick<interval){  \
				return;                                  \
			}                                            \
            display_tick = 0;
            
void App_display_password_task(){
	OLED_Clear();
	//使用switch来显示每个g_LockState要显示的内容
	switch(g_LockState){
		case LockState_Default:
			OLED_ShowString(1,1,"Locked");
			break;
		case LockState_Input:
			OLED_ShowString(1,1,"Locked");
			OLED_ShowString(2,1,"Enter PIN:");
			//根据现有密码长度，显示相应数量的*
			uint8_t password_len = strlen(g_password);
			//printf("g_password[%d]: %s",password_len,g_password);
			//OLED_ShowString(3,2,"      ");
			for(uint8_t i=0;i<password_len;i++){
				OLED_ShowChar(3,2+i,'*');
			}
			break;
		case LockState_Error:
			//显示 Try Again
			OLED_ShowString(1,1,"Try Again");
			//显示剩余次数
			sprintf(strbuf,"%d chance to try",g_password_error_count);
			OLED_ShowString(2,1,strbuf);
			//将状态改回去 计时1500ms
//			 printf("sj111\r\n");
//           INTERVAL_CHECK(1500); //不是不到间隔时间不往下执行，为什么一直在这里闪退出去，直接往下执行了？
//			 printf("sj2\r\n");
			Delay_check(1500);
            
			g_LockState = LockState_Input; // 切换到输入状态
			break;
		case LockState_Locked:
			//OLED_ShowString(1,1,"locked");
			//显示N秒后再重试
			//倒计时10秒
			if(display_tick == 0){
				display_tick = tasks_tick_get();
				return;
			}
			if(tasks_tick_get() - display_tick > 1000){
				display_tick = 0;
				sec_display--;
				if(sec_display == 0){
					sec_display = 10;
					g_LockState = LockState_Default; // 切换到默认状态
				}
			}
			sprintf(strbuf,"%dafter to try",sec_display);
			OLED_ShowString(1,1,strbuf);
			break;
		case LockState_Unlock:
			//OLED_ShowString(1,1,"unlock");
			//显示 成功解锁
			OLED_ShowString(1,1,"Unlocked Success");
			//2秒后返回默认状态，同上面延时
			if(display_tick == 0){
				display_tick = tasks_tick_get();
				return;
			}
			if(tasks_tick_get() - display_tick > 2000){
				display_tick = 0;
				g_LockState = LockState_Default; // 切换到默认状态
			}
			break;
	}

}
void App_display_suspend_task(){
	printf("任务暂停，执行清屏操作\r\n");
}





